﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using COM;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace EncryptionServer
{
    static class Program
    {
        public static bool _serverChange;
        public static bool _notFoundDog;

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //EnstryptMessage.EncryptConfiguration();
            IsThisProcessRunning();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ApplicationExit += new EventHandler(Restart);
            bool isDebug = false;
#if DEBUG
            isDebug = true;             
#endif
            if (!isDebug)
            {
                RegDLL();     
            }
            bool bCreatedNew;
            Mutex m = new Mutex(false, "EncryptionServer", out bCreatedNew);

            if (bCreatedNew)
            {
                //MessageBox.Show("1111", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                try
                {

              
                Application.Run(new FrmMain());
                if (_notFoundDog)
                {
                    MessageBox.Show("找不到加密狗，无法继续运行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                }
                catch (Exception ex) 
                {

                    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
        }

        private static void IsThisProcessRunning()
        {
            System.Diagnostics.Process[] pTemp = System.Diagnostics.Process.GetProcesses();
            int num = 0;
            foreach (System.Diagnostics.Process pTempProcess in pTemp)
            {

                if ((pTempProcess.ProcessName.ToLower()) == ("EncryptionServer.exe").ToLower() || (pTempProcess.ProcessName.ToLower()) == ("EncryptionServer").ToLower())
                {
                    num++;
                    if (num > 1)
                    {
                        MessageBox.Show("服务端程序已经开启,不允许开启多个该程序", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Environment.Exit(0);
                    }
                }
            }
        }
        static void RegDLL()
        {
            Process.Start("regsvr32", string.Format("/s \"{0}\"", Path.Combine(Environment.CurrentDirectory, "ImageEnXLibrary.ocx")));
            Process.Start("regsvr32", string.Format("/s \"{0}\"", Path.Combine(Environment.CurrentDirectory, "ET199Com.dll")));
            //Process.Start("regsvr32", string.Format("/s \"{0}\"", Path.Combine(Environment.CurrentDirectory, "LayoutDll.dll")));
            //Process.Start("regsvr32", string.Format("/s \"{0}\"", Path.Combine(Environment.CurrentDirectory, "RealLayoutPanel.ocx")));
            //Process.Start("regsvr32", string.Format("/s \"{0}\"", Path.Combine(Environment.CurrentDirectory, "MarkImageDll.dll")));

        }
        //默认重启
        private static void Restart(object sender, EventArgs e)
        {

            if (_serverChange)
            {
                Process.Start(Application.ExecutablePath);
            }
        }

    }
}
